"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearAllCommunicationAccountState = exports.Axios = void 0;
const log4js_1 = require("log4js");
const fetch_1 = require("./fetch");
const logger = (0, log4js_1.getLogger)('account-api/api/');
function normalized(url, type) {
    if (url) {
        console.log('url: => ', url);
        return (url === null || url === void 0 ? void 0 : url.startsWith('artemis')) ? url : `/artemis-${type}/${url.split('/').pop()}`;
    }
    return url;
}
function normalizedChat(url) {
    return url && normalized(url, 'chat');
}
function normalizedRecord(url) {
    return url && normalized(url, 'record');
}
class Axios {
    constructor(token) {
        this.basic = new fetch_1.Basic(token);
    }
    getAccount() {
        return this.basic.post('pubCommunicationAccount/list')
            .then(res => res && res.json());
    }
    getCommunicationGroup() {
        return this.basic.post('pubDistributionGroup/list', { body: { type: 0 } })
            .then(res => res && res.json());
    }
    toggleAccountState(account, status) {
        const body = {
            account,
            status
        };
        return this.basic.post('pubCommunicationAccount/resetState', { body })
            .then(res => res && res.json());
    }
    loginRecord({ userId, username, upperTime, downTime, }) {
        const body = {
            communicationAccount: userId,
            personName: username,
            upperTime: upperTime + '',
            downTime: downTime + '',
            onlineTime: downTime - upperTime + '',
        };
        return this.basic.post('pubLogin/insert', { body })
            .then(res => res && res.json());
    }
    chatMessageRecord({ userId, username, roomId, roomName, content, contentType, }) {
        const body = {
            "communicationAccount": userId,
            "userName": username,
            "roomId": roomId,
            "groupName": roomName,
            "contentPath": content,
            "contentType": contentType,
            "messageTime": Date.now().toString(),
            "startTime": Date.now().toString(),
        };
        console.log('发送聊天消息： =》 ', body);
        return this.basic.post('pubMessage/insert', { body })
            .then(res => res && res.json());
    }
    callLogged({ classId, senderId, senderName, receives, chatMode, callTime, content, }) {
        const body = {
            "groupId": classId,
            "callTime": callTime + '',
            "callDuration": Date.now() - callTime + '',
            "chatMode": chatMode,
            "contentPath": content,
            "mainAccount": senderId,
            "mainName": senderName,
            "calledAccount": receives.map(({ userId }) => userId).join(),
            "calledName": receives.map(({ username }) => username).join(),
        };
        if (Array.isArray(content)) {
            body.contentPath = content.map(normalizedRecord).join();
        }
        else {
            body.contentPath = normalizedRecord(content);
        }
        logger.debug('============== 保存通话记录 ================', body);
        return this.basic.post('pubCall/insert', { body })
            .then(res => res && res.json());
    }
    intercomRecord({ classId, classname, userId, username, content, }) {
        const body = {
            distributionGroupId: classId,
            distributionGroupName: classname,
            accountId: userId,
            accountName: username,
            contentPath: normalizedRecord(content),
        };
        logger.debug('============== 保存对讲记录 ================', body);
        return this.basic.post('pubIntercomRecord/insert', { body })
            .then(res => res && res.json())
            .catch(console.error);
    }
}
exports.Axios = Axios;
function clearAllCommunicationAccountState() {
    return fetch_1.AxiosBase.fetch('/pubCommunicationAccount/resetAllState', { method: 'POST', body: "{}" })
        .then(res => res && res.json());
}
exports.clearAllCommunicationAccountState = clearAllCommunicationAccountState;
